package com.sevlet.setting;

import com.Bean.Consumer;
import com.dao.ConsumerDao;
import com.dao.EmployeeTwoDao;
import com.dao.impl.ConsumerDaoImpl;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sevlet.BaseSevlet;
import com.util.JsonUtil;
import com.util.PageUtil;
import com.util.ProxyUtil;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/ConsumerServlet")
public class ConsumerServlet extends BaseSevlet {

    public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ConsumerDao consumerDao = new ConsumerDaoImpl();

        Consumer consumer = new Consumer();
        resp.setContentType("text/html;charset=utf-8");
        JsonUtil jsonUtil = new JsonUtil();
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat("yyyy-MM-dd");
        Gson gson = gsonBuilder.create();
        PrintWriter out = null;


        //System.out.println(req.getParameter("bookInfo"));

        try {
            out = resp.getWriter();


        } catch (IOException e) {
            e.printStackTrace();
        }
        String curentPage =   req.getParameter("currentPage");


        try {
            PageUtil<Consumer> employeePageUtil =  consumerDao.queryDataBypage(Integer.parseInt(curentPage),15,consumer);

            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");
            System.out.println(employeePageUtil);
            jsonUtil.setData(employeePageUtil);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }  finally {
            out.print(gson.toJson(jsonUtil));
        }
    }

    public void insertData(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ConsumerDao consumerDao = new ConsumerDaoImpl();
        ConsumerDao daoProxy = (ConsumerDao) ProxyUtil.getDaoProxy(consumerDao);

        JsonUtil jsonUtil = new JsonUtil();
        try {
            Consumer consumer = new Consumer();

            System.out.println(req.getParameter("eid"));

            //将客户端传递过来的请求参数封装到bookBean对象中
            //要求：参数名必需与bookBean中的属性名有对应关系
            System.out.println("反射前的BookBean对象中的属性值为:"+consumer);
            BeanUtils.populate(consumer,req.getParameterMap());
            System.out.println("反射后的BookBean对象中的属性值为:"+consumer);

            int res =  daoProxy.insert(consumer);

            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");

        } catch (SQLException e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:"+e.getMessage());
        }
        catch (Exception e){
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:"+e.getMessage());
        }

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson =  gsonBuilder.create();

        String jsonStr =  gson.toJson(jsonUtil);


        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print(jsonStr);
    }

    public void updateData(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ConsumerDao consumerDao = new ConsumerDaoImpl();
        ConsumerDao daoProxy = (ConsumerDao) ProxyUtil.getDaoProxy(consumerDao);
        JsonUtil jsonUtil = new JsonUtil();
        try {
            Consumer consumer = new Consumer();

            //将客户端传递过来的请求参数封装到bookBean对象中
            //要求：参数名必需与bookBean中的属性名有对应关系
            System.out.println("反射前的BookBean对象中的属性值为:"+consumer);
            BeanUtils.populate(consumer,req.getParameterMap());
            System.out.println("反射后的BookBean对象中的属性值为:"+consumer);

            int res =  daoProxy.update(consumer);

            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");

        } catch (SQLException e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:"+e.getMessage());
        }
        catch (Exception e){
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:"+e.getMessage());
        }

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson =  gsonBuilder.create();

        String jsonStr =  gson.toJson(jsonUtil);


        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print(jsonStr);
    }

    public void deleteData(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ConsumerDao consumerDao = new ConsumerDaoImpl();
        ConsumerDao daoProxy = (ConsumerDao) ProxyUtil.getDaoProxy(consumerDao);
        JsonUtil jsonUtil = new JsonUtil();
        try {
            Consumer consumer = new Consumer();

            //将客户端传递过来的请求参数封装到bookBean对象中
            //要求：参数名必需与bookBean中的属性名有对应关系
            System.out.println("反射前的BookBean对象中的属性值为:"+consumer);
            BeanUtils.populate(consumer,req.getParameterMap());
            System.out.println("反射后的BookBean对象中的属性值为:"+consumer);

            int res =  daoProxy.deleteById(consumer.getGid());

            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");

        } catch (SQLException e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:"+e.getMessage());
        }
        catch (Exception e){
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:"+e.getMessage());
        }

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson =  gsonBuilder.create();

        String jsonStr =  gson.toJson(jsonUtil);


        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print(jsonStr);
    }
}
